<?php

// include 'Upload.class.php';
// if (!empty($_FILES)) {
// 	upload::uploadFile('upload');
// }

//使用前提是在点击了上传的情况下
class Upload {
								//定义属性
	//保存路径
	protected $savePath;
	//保存文件名称
	protected $fileName;
	//文件后缀名
	protected $extension;
	//是否使用日期目录
	protected $dateDir;
	//是否使用随机名字
	protected $randName;
	//允许的MIME
	protected $mimes = ['image/png','image/jpeg','image/gif'];
	//允许的后缀
	protected $suffixes = ['png','jpg','jpeg','gif'];
	//错误代码
	protected $errno = 0;
	//错误信息
	protected $errmsg;
	//存储上传表单信息
	protected $uploadInfo;

								//构造方法
	protected function __construct($savePath,$dateDir,$randName) {
		$this -> savePath = rtrim($savePath,'/') . '/';
		$this -> dateDir = $dateDir;
		$this -> randName = $randName;
	}



								//__get方法
	function __get($varName) {
		if ($varName=='errmsg') {
			return $this -> errmsg;
		} else {
			return '没有该属性';
		}
	}


								//静态方法
	static function uploadFile($file,$savePath='./',$dateDir=true,$randName=true) {
		$obj = new self($savePath,$dateDir,$randName);
		$obj -> checkAndUpload($file);

		if ($obj -> errno != 0) {
			echo $obj -> errmsg;
		}
	}




								//上传文件（主操作） 传入文件表单的 name
	protected function checkAndUpload($file) {
		//获得文件信息
		$this -> uploadInfo = $_FILES[$file];

		//检查保存路径
		$this -> checkSavePath();
			if ($this -> errno!=0) {
				return false;
			}
		//检查上传文件信息
		$this -> checkUploadInfo();
			if ($this -> errno!=0) {
				return false;
			}
		//检查文件格式
		$this -> checkFileFormat();
			if ($this -> errno!=0) {
				return false;
			}
		//检查是否是上传文件
		$this -> checkIsUploadFile();
			if ($this -> errno!=0) {
				return false;
			}
		//拼接路径名
		$this -> joinPathName();
		//移动上传文件
		$this -> moveUploadFile();
		if ($this -> errno!=0) {
				return false;
			}
		return true;
	}



								//用于各项检查的方法
	protected function checkSavePath() {
		if (!is_writable($this -> savePath)) {
			$this -> errno = -2;
			$this -> errmsg = '保存路径不可写';
		}
		if (!is_dir($this -> savePath)) {
			$this -> errno = -1;
			$this -> errmsg = '保存路径不存在';
		}
	}
	protected function checkUploadInfo() {
		if ($this->uploadInfo['error'] != 0) {
			switch ($this->uploadInfo['error']) {
				case UPLOAD_ERR_INI_SIZE:	
					$this->errmsg = '超出了配置文件中设定文件大小';
					break;
				case UPLOAD_ERR_FORM_SIZE:
					$this->errmsg = '超出了规定的大小';
					break;
				case UPLOAD_ERR_PARTIAL:
					$this->errmsg = '只有部分文件被上传';
					break;
				case UPLOAD_ERR_NO_FILE:
					$this->errmsg = '没有文件被上传';
					break;
				case UPLOAD_ERR_NO_TMP_DIR:
					$this->errmsg = '没有找到临时文件夹';
					break;
				case UPLOAD_ERR_CANT_WRITE:
					$this->errmsg = '文件写入失败';
					break;
				default:
					$this->errmsg = '未知错误';
					break;
			}
		}
		$this->errno = $this->uploadInfo['error'];
	}
	protected function checkFileFormat() {
		$this -> extension = pathinfo($this -> uploadInfo['name'], PATHINFO_EXTENSION);
		if (!in_array($this -> extension,$this -> suffixes)) {
			$this -> errno = -4;
			$this -> errmsg = '不支持的文件后缀' . $this -> extension;
		}
		if (!in_array($this -> uploadInfo['type'],$this -> mimes)) {
			$this -> errno = -3;
			$this -> errmsg = '不支持的MIME' . $this -> uploadInfo['type'];
		}
	}
	protected function checkIsUploadFile() {
		if (!is_uploaded_file($this -> uploadInfo['tmp_name'])) {
			$this -> errno = -5;
			$this -> errmsg = '不是上传文件';
		}
	}



								//拼接路径名
	protected function joinPathName() {
		//路径
		if ($this -> dateDir) {
			$this -> savePath .= date('Y/m/d/');
			if (!file_exists($this -> savePath)) {     //如果目录不存在  创建一个
				mkdir($this -> savePath,0777,true);
			}
		}
		//文件名
		$this -> fileName = $this -> savePath;
		if ($this -> randName) {
			$this -> fileName .= uniqid();
		} else {
			$this -> fileName .= pathinfo($this -> uploadInfo['name'],PATHINFO_FILENAME);
		}
		//后缀
		$this -> fileName .= '.' . $this -> extension;
	}



								//移动上传文件
	protected function moveUploadFile() {
		if (!move_uploaded_file($this -> uploadInfo['tmp_name'],$this -> fileName)) {
			$this -> errno = -6;
			$this -> errmsg = '上传文件保存失败';
		}
	}
								//
}